package com.lrs.java.j2se_advanced.codec;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Random;
import java.util.Scanner;

import org.junit.Test;

public class DataInputTest {

	private String utf8_fileName = "readme_UTF-8.txt";
	private String gbk_fileName = "readme_default.txt";
	
	private String classpath_fileName = "classpath_UTF-8.txt";
	
	@Test
	public void testWriteBytes() throws Exception {
		String str = "中hello";
		
		DataOutputStream out = new DataOutputStream(new FileOutputStream("test.dat"));
//		DataOutputStream out = new DataOutputStream(System.out);
		//discard the high-order eight bits
		out.writeBytes(str);
		
		out.size();
		
//		System.out.println("hello");
		out.flush();
	}
	
	@Test
	public void testDataWriter() throws Exception {
		
		//写入文件的是字节流`
		DataOutputStream out = new DataOutputStream(
//				new BufferedOutputStream(
					new FileOutputStream("test.dat")
//				)
			);
		
		Random radom = new Random(2);
		for(int i=0; i<1; i++) {
			Long d = new Long(radom.nextInt());
			out.writeLong(d);
		}
		out.close();
	}
	
	@Test
	public void testDataReader() throws Exception {
		DataInputStream input = new DataInputStream(new FileInputStream("test.dat"));
		long l = input.readLong();
		System.out.println(l);
		
	}
	
	@Test
	public void testPrintWriter() throws Exception {
		PrintWriter writer = new PrintWriter(
//				new OutputStreamWriter(new FileOutputStream("test.dat"), "GBK")
				"test.dat"
			);
		String str = "123";
		byte[] b = str.getBytes();
		writer.write(str);
		writer.flush();
	}
	
	
	@Test
	public void testReader() throws Exception {
		InputStreamReader utf8_isr = new InputStreamReader(new FileInputStream(utf8_fileName));
		BufferedReader reader = new BufferedReader(utf8_isr);
		String str;
		while((str=reader.readLine()) != null) {
			System.out.println(str);
		}
		
		InputStreamReader gbk_isr = new InputStreamReader(new FileInputStream(gbk_fileName), "GBK");
		BufferedReader gbk_reader = new BufferedReader(gbk_isr);
		 
		while((str=gbk_reader.readLine()) != null) {
			System.out.println(str);
		}
		reader.close();
		gbk_reader.close();
		
	}
	
	@Test
	public void testScanner() throws Exception {
		Scanner scanner = new Scanner(new File("test.dat"));
//		String v = scanner.nextLine();
//		System.out.println(v);
		Long l = scanner.nextLong();
		System.out.println(l);
	}
	
	@Test
	public void testResourceStream() {
		// 以'/'开头的，指向 类路径的根目录
		Scanner scanner = new Scanner(getClass().getResourceAsStream("/"+classpath_fileName));
		
		if(scanner.hasNextLine()) {
			System.out.println(scanner.nextLine());
		}
		
		scanner = new Scanner(getClass().getResourceAsStream("/com/lrs/java/j2se_advanced/" + classpath_fileName));
		if(scanner.hasNextLine()) {
			System.out.println(scanner.nextLine());
		}
		
		// the absolute name is of the following form:  modified_package_name/name
		scanner = new Scanner(getClass().getResourceAsStream(classpath_fileName));
		if(scanner.hasNextLine()) {
			System.out.println(scanner.nextLine());
		}
		
		scanner = new Scanner(getClass().getResourceAsStream("../" + classpath_fileName));
		if(scanner.hasNextLine()) {
			System.out.println(scanner.nextLine());
		}
		
		ClassLoader loader = Thread.currentThread().getContextClassLoader();
		InputStream isr = loader.getResourceAsStream("/" + classpath_fileName);
		scanner = new Scanner(isr);
		if(scanner.hasNextLine()) {
			System.out.println(scanner.nextLine());
		}
	}
	
	@Test
	public void testPath() {
		System.out.println(Thread.currentThread().getContextClassLoader()
				.getResource(""));
		System.out.println(getClass().getClassLoader().getResource(""));
		System.out.println(ClassLoader.getSystemResource(""));
		System.out.println(getClass().getResource(""));
		System.out.println(getClass().getResource("/")); // Class文件所在路径
		System.out.println(new File("/").getAbsolutePath());
		System.out.println(System.getProperty("user.dir"));
	}
	
	
	
}
